<template>
  <div class="customer">
    <div class="search blockMould">
      <div class="left">
        <!--标签分类-->
        <div class="search_list">
          <span class="search_text">标签分类：</span>
          <el-select size="small" v-model="labelClassification" value-key="resourceValue" placeholder="请选择">
            <el-option v-for="item in labelClassificationList" :key="item.resourceValue" :label="item.resourceItem"
                       :value="item"></el-option>
          </el-select>
        </div>
      </div>
      <div class="center">
        <!--标签名称-->
        <div class="search_list">
          <span class="search_text">标签名称：</span>
          <el-input size="small" placeholder="输入标签名称" v-model="labelName"></el-input>
        </div>
      </div>
      <div class="right"></div>
    </div>
    <!--基础标签列表-->
    <div class="list blockMould">
      <div class="list_head_button">
        <div class="search_button">
          <el-button size="mini" type="warning" plain @click="add_label()">新增</el-button>
        </div>
        <div class="search_name">
          <el-button size="mini" type="primary" @click="tag_base_info()">查询</el-button>
          <el-button size="mini" plain @click="reset()">重置</el-button>
        </div>
      </div>
      <el-table
        :header-cell-style="{background:'#F6F7FB',color:'#666666',height: '50px'}"
        :row-class-name="tableRowClassName"
        :data="basicLabelList">
        <el-table-column
          prop="id"
          label="标签ID"
          min-width="25">
        </el-table-column>
        <el-table-column
          prop="tagClassifyName"
          label="分类"
          min-width="33">
        </el-table-column>
        <el-table-column
          prop="tagName"
          label="标签名称"
          min-width="33">
        </el-table-column>
        <el-table-column
          prop="tagType"
          label="样式"
          min-width="30">
          <template slot-scope="scope">
            <div>
              <span v-if="scope.row.tagType=='radio'">单选</span>
              <span v-else-if="scope.row.tagType=='checkbox'">多选</span>
              <span v-else-if="scope.row.tagType=='text'">文本</span>
              <span v-else-if="scope.row.tagType=='number'">数字</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="15">
          <template slot-scope="scope">
            <div>
              <el-tooltip popper-class="operation_button_promit" effect="dark" content="编辑" placement="bottom">
                <el-button type="text" @click="edit(scope.$index, scope.row)">
                  <img src="../../assets/common/edit.svg" alt="">
                </el-button>
              </el-tooltip>
              <el-tooltip popper-class="operation_button_promit" effect="dark" content="删除" placement="bottom">
                <el-button type="text" @click="tag_base_delete(scope.$index, scope.row)">
                  <img src="../../assets/common/delete.svg" alt="">
                </el-button>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination :page="page" @changeCurrentPage="handleCurrentChange" @changePageSize="handlePageSize"/>
    </div>
    <!--添加基础标签-->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="526px"
      class="label_dialog">
      <div>
        <div class="search_list">
          <span class="search_text">标签分类：</span>
          <el-select size="small" v-model="labelClassification2" value-key="resourceValue" placeholder="请选择">
            <el-option v-for="item in labelClassificationList2" :key="item.resourceValue" :label="item.resourceItem"
                       :value="item"></el-option>
          </el-select>
        </div>
        <div class="search_list">
          <span class="search_text">标签名称：</span>
          <el-input size="small" placeholder="输入标签名称" v-model="labelName2"></el-input>
        </div>
        <div class="search_list">
          <span class="search_text">标签样式：</span>
          <el-select size="small" v-model="labelStyle" placeholder="请选择">
            <el-option v-for="item in labelStyleList" :key="item.id" :label="item.tagType"
                       :value="item.id"></el-option>
          </el-select>
        </div>
        <div v-if="optionValue" class="select_value">
          <span class="search_text">选项值：</span>
          <div class="value">
            <div class="search_list" v-for="(value,index) in list">
              <el-input size="small" placeholder="输入选项值" v-model="value.optionName"></el-input>
              <i class="el-icon-circle-plus-outline icon" style="color: #5687FF" @click="tag_base_add" v-if="value.isShowAdd"></i>
              <i class="el-icon-circle-close icon" style="color:#EA5E32" @click="deletes(index,value)" v-if="value.isShowDelete"></i>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="Visible =='提交'">确定</el-button>
        <el-button type="primary" @click="confirmation()" v-if="Visible =='确认'">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {api, baseAjax} from '../../api/api';
  import Cookies from 'js-cookie';
  import {statusCode} from '../../utils/index';
  import pagination from '../public/pagination';
  import {formatDate} from '../../utils/validate';
  import phone_call from '../../element/phone_call';

  export default {
    name: 'myCustomer',
    components: {
      pagination,
      phone_call
    },
    computed: {},
    data() {
      return {
        /*标签分类*/
        labelClassification: '',
        labelClassificationList: [],
        labelClassification2: {},
        labelClassificationList2: [],
        /*标签名称*/
        labelName: '',
        labelName2: '',
        /*基础标签列表*/
        basicLabelList: [
          {
            "callId": 9669,
            "doctorName": "欧阳昀",
            "techTitle": "主治医师",
            "hospitalName": "海军总医院",
            "depart": "泌尿外科",
            "productName": "惠每测试产品1",
            "repName": "陈颐",
            "purposeItem": "招募医生",
            "resultItem": "医生拒绝",
            "callStatus": 1,
            "operationTime": "2020-12-02 10:58:36",
            "callDuration": 70,
            "tapeUri": "https://huimei-vr-dev.oss-cn-shanghai.aliyuncs.com/cloud_sale/2020-12-02/mp3/000d7db4-8ab3-4671-8372-6f666ca682a5.mp3",
            "vrid": 7188
          },
          {
            "callId": 7570,
            "doctorName": "殷中玲",
            "techTitle": "主治医师",
            "hospitalName": "宿迁市人民医院",
            "depart": "泌尿外科",
            "productName": "惠每测试产品1",
            "repName": null,
            "purposeItem": "招募医生",
            "resultItem": "医生拒绝",
            "callStatus": 1,
            "operationTime": "2020-11-27 16:00:26",
            "callDuration": 9,
            "tapeUri": "https://huimei-vr-dev.oss-cn-shanghai.aliyuncs.com/cloud_sale/2020-11-27/mp3/004391ef-68c5-43f7-ae98-38db14eec404.mp3",
            "vrid": 4194
          },
          {
            "callId": 1840,
            "doctorName": "朱建坦",
            "techTitle": "副主任医师",
            "hospitalName": "柳州市柳铁中心医院",
            "depart": "泌尿外科",
            "productName": "惠每测试产品1",
            "repName": "19",
            "purposeItem": "招募医生",
            "resultItem": "医生拒绝",
            "callStatus": 1,
            "operationTime": "2020-11-04 15:54:49",
            "callDuration": 21,
            "tapeUri": "https://huimei-vr-dev.oss-cn-shanghai.aliyuncs.com/cloud_sale/2020-11-04/mp3/00457a3f-860e-4f9a-864b-0b6f177db846.mp3",
            "vrid": 859
          },
        ],
        //分页
        page: {
          currentPage: 1,    //当前页
          pageSize: 10,    //每页显示条数
          totalPage: 0,   //总页数
          total:null   //总条数
        },
        //添加基础标签
        dialogVisible: false,
        Visible: '',
        //标签样式
        labelStyle: 'radio',
        labelStyleList: [
          {
            id: 'radio',
            tagType: '单选',
          }, {
            id: 'checkbox',
            tagType: '多选',
          }, {
            id: 'text',
            tagType: '文本',
          }, {
            id: 'number',
            tagType: '数字',
          }
        ],
        //选项值
        optionValue: true,
        list: [
          {
            id: '',
            optionName: '',
            optionType: '',
            tagId: '',
            isShowAdd:true,
            isShowDelete:false
          }
        ],
        tagId: '',
        title:'新增'
      }
    },
    watch: {
      labelStyle() {
        if (this.labelStyle == 'radio' || this.labelStyle == 'checkbox') {
          this.optionValue = true
          this.list = [
            {
              id: '',
              optionName: '',
              optionType: '',
              tagId: '',
              isShowAdd:true,
              isShowDelete:false
            }
          ]
        } else {
          this.optionValue = false
          this.list = []
        }
      },
      dialogVisible() {
        if (this.dialogVisible == true) {
          this.labelClassification2 = {}
          this.labelName2 = ''
          this.list = [
            {
              id: '',
              optionName: '',
              optionType: '',
              tagId: '',
              isShowAdd:true,
              isShowDelete:false
            }
          ]
        }
      }
    },
    mounted() {
      this.tag_base_info()
      this.get_resources()
    },
    methods: {
      //修改列表tr颜色
      tableRowClassName({row, rowIndex}) {
        if (rowIndex % 2 === 1) {
          return 'success_row';
        }
        return '';
      },
      //查看基础标签
      tag_base_info() {
        let data = {
          "id": '',
          "sysStatus": "",
          "tagClassifyId": '',
          "tagClassifyName": "",
          "tagName": this.labelName,
          "tagType": "",
          "currentPage": this.page.currentPage,
          "pageSize": this.page.pageSize,
        }
        if (this.labelClassification) {
          data.tagClassifyId = this.labelClassification.resourceValue
          data.tagClassifyName = this.labelClassification.resourceItem
        }
        baseAjax(api.tag_base_info, data, (res) => {
          if (res.code == 200) {
            this.basicLabelList = res.data.tagBaseList
            this.page.totalPage = res.data.totalPage
            this.page.total = res.data.count
          }
        })
      },
      //获取资源表相应资源内容/resource/getReourceItem/{resource_name}
      get_resources() {
        let getReourceItem = {
          url: '/resource/getReourceItem/tag_base_type',
          method: 'GET'
        }
        baseAjax(getReourceItem, '', (res) => {
          if (res.code == 200) {
            this.labelClassificationList = res.data
            this.labelClassificationList2 = res.data
          }
        })
      },
      //改变当前页码
      handleCurrentChange(currentPage) {
        this.page.currentPage = currentPage;
        this.tag_base_info()
      },
      //改变每页条数
      handlePageSize(pageSize){
        this.page.pageSize = pageSize;
        this.tag_base_info();
      },
      //重置
      reset() {
        this.labelClassification = ''
        this.labelName = ''
        this.tag_base_info()
      },
      //添加基础标签
      add_label() {
        this.dialogVisible = true;
        this.Visible = '提交'
      },
      //添加选项值
      tag_base_add() {
        this.list.push({
          id: '',
          optionName: '',
          optionType: '',
          tagId: '',
          isShowAdd:false,
          isShowDelete:true
        })
      },
      //删除选项值
      deletes(index, value) {
        if (this.list.length > 1) {
          this.list.splice(index, 1);
        }
      },
      //添加基础标签-提交
      submit() {
        if (this.labelStyle == 'radio' || this.labelStyle == 'checkbox') {
          let val;
          for (let res of this.list) {
            if (res.optionName == '') {
              this.$message.warning('选项值不能为空！');
              val = false
              break;
            } else {
              val = true
            }
          }
          if (val == true) {
            let data = {
              tagClassifyId: this.labelClassification2.resourceValue,
              tagClassifyName: this.labelClassification2.resourceItem,
              tagId: '',
              tagName: this.labelName2,
              tagType: this.labelStyle,
              tagBaseOptionParams: this.list
            }
            let tag_base_create = {
              url: '/tag_base/tag_base_create',
              method: 'POST'
            }
            baseAjax(tag_base_create, data, (res) => {
              if (res.code == 200) {
                if (res.data == true) {
                  this.dialogVisible = false;
                  this.labelClassification2 = ''
                  this.labelName2 = ''
                  this.labelStyle = ''
                  this.list = [
                    {
                      id: '',
                      optionName: '',
                      optionType: '',
                      tagId: '',
                      isShowAdd:true,
                      isShowDelete:false
                    }
                  ]
                  this.$message.success('添加成功！');
                  this.tag_base_info()
                } else {
                  this.$message.error('添加失败！');
                }
              }
            })
          }
        } else {
          let data = {
            tagClassifyId: this.labelClassification2.resourceValue,
            tagClassifyName: this.labelClassification2.resourceItem,
            tagId: '',
            tagName: this.labelName2,
            tagType: this.labelStyle,
            tagBaseOptionParams: this.list
          }
          let tag_base_create = {
            url: '/tag_base/tag_base_create',
            method: 'POST'
          }
          baseAjax(tag_base_create, data, (res) => {
            if (res.code == 200) {
              if (res.data == true) {
                this.dialogVisible = false;
                this.labelClassification2 = ''
                this.labelName2 = ''
                this.labelStyle = ''
                this.list = [
                  {
                    id: '',
                    optionName: '',
                    optionType: '',
                    tagId: '',
                    isShowAdd:true,
                    isShowDelete:false
                  }
                ]
                this.$message.success('添加成功！');
                this.tag_base_info()
              } else {
                this.$message.error('添加失败！');
              }
            }
          })
        }
      },
      //编辑
      edit(index, value) {
        this.dialogVisible = true;
        this.Visible = '确认'
        this.labelClassification2 = {}
        this.labelName2 = []
        this.title = '编辑'
        this.list = [
          {
            id: '',
            optionName: '',
            optionType: '',
            tagId: '',
            isShowAdd:true,
            isShowDelete:false
          }
        ]
        let tag_option_info_base = {
          url: '/tag_base/tag_option_info_base?tagId=' + value.id,
          method: 'GET'
        }
        baseAjax(tag_option_info_base, '', (res) => {
          if (res.code == 200) {
            console.log(res)
            this.labelClassification2 = res.data.tagClassifyName
            // this.labelClassification2 = {
            //   resourceValue: res.data.tagClassifyId,
            //   resourceItem: res.data.tagClassifyName,
            // }
            this.labelName2 = res.data.tagName
            this.labelStyle = res.data.tagType
            this.tagId = res.data.id
            this.list = res.data.tagBaseOptionBeanList
            this.list.forEach(item=>{
              item.isShowAdd = false;
              item.isShowDelete = true;
            })
            this.list[0].isShowAdd = true
            this.list[0].isShowDelete = false
          }
        })
      },
      //编辑-确认
      confirmation() {
        if (this.labelStyle == 'radio' || this.labelStyle == 'checkbox') {
          let val;
          for (let res of this.list) {
            if (res.optionName == '') {
              this.$message.warning('选项值不能为空！');
              val = false
              break;
            } else {
              val = true
            }
          }
          if (val == true) {
            let data = {
              tagClassifyId: '',
              tagClassifyName: '',
              tagId: this.tagId,
              tagName: this.labelName2,
              tagType: this.labelStyle,
              tagBaseOptionParams: this.list
            }
            if (typeof (this.labelClassification2) == 'object') {
              data.tagClassifyId = this.labelClassification2.resourceValue
              data.tagClassifyName = this.labelClassification2.resourceItem
            } else {
              this.labelClassificationList2.map((res) => {
                if (this.labelClassification2 == res.resourceItem) {
                  data.tagClassifyId = res.resourceValue
                  data.tagClassifyName = res.resourceItem
                }
              })
            }
            let tag_base_update = {
              url: '/tag_base/tag_base_update',
              method: 'POST'
            }
            baseAjax(tag_base_update, data, (res) => {
              if (res.code == 200) {
                if (res.data == true) {
                  this.dialogVisible = false;
                  this.labelClassification2 = ''
                  this.labelName2 = ''
                  this.labelStyle = ''
                  this.list = [
                    {
                      id: '',
                      optionName: '',
                      optionType: '',
                      tagId: '',
                      isShowAdd:true,
                      isShowDelete:false
                    }
                  ]
                  this.$message.success('修改成功！');
                  this.tag_base_info()
                } else {
                  this.$message.error('修改失败！');
                }
              }
            })
          }
        }
      },
      //删除基础标签/tag_base/tag_base_delete
      tag_base_delete(index, row) {
        this.$confirm('是否确认删除?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let tag_base_delete = {
            url: '/tag_base/tag_base_delete?tagId=' + row.id,
            method: 'POST'
          }
          baseAjax(tag_base_delete, '', (res) => {
            if (res.code == 200) {
              this.tag_base_info()
              if (res.data == true) {
                this.$message.success('删除成功！');
              } else {
                this.$message.error('删除失败！');
              }
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消删除'
          });
        });
      },
    }
  }
</script>

<style lang="less" scoped>
  .customer {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    .search{
      .left,.center{
        margin-right:51px;
      }
    }
  }
</style>
